package com.lanou3g.code0516;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@WebServlet(name = "LoginServlet",urlPatterns = "/login")
public class LoginServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //TODO 获取数据
        String username =request.getParameter("username");

        String msg = "";
        //校验
        if ("root".equals(username)){
            //登录成功
            msg="登陆成功";
            //把用户信息存到session中去
            //获取session对象
            HttpSession session=request.getSession();
            //把用户信息存储到session中,注意:晚上可以把user对象,直接
            //存入session
            session.setAttribute("username",username);
            session.setAttribute("word","人生只有走出来的美丽");

        }else{
            //登录失败
            msg="登录失败";
        }
        request.setAttribute("msg",msg);
        request.getRequestDispatcher("/user/msg.jsp").forward(request,response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
     this.doPost(request,response);
    }
}
